<template>
  <!-- 任务管理  -->
  <div class="rule-box">
    <div v-if="$route.query.name" class="back-title">
      <i class="el-icon-arrow-left" @click="$router.go(-1)">返回</i>
    </div>
    <div class="app-container">
      <el-form ref="queryForm" :model="queryParams" size="small" :inline="true" @submit.native.prevent>
        <el-form-item label="任务名称" prop="taskName">
          <el-input
            v-model="queryParams.taskName"
            placeholder="请输入任务名称"
            clearable
            style="width: 240px"
            @keyup.enter.native="getList"
          />
        </el-form-item>
        <el-form-item label="接入数据源类型" prop="productCode">
          <el-cascader
            v-model="temInputDataSourceType"
            :options="productData"
            :props="propsData"
            :show-all-levels="false"
            clearable
            filterable
            @change="handleChange"
          />
        </el-form-item>
        <el-form-item label="输出数据源类型" prop="productCode">
          <el-cascader
            v-model="temOutputDataSourceType"
            :options="exportsData"
            :props="propsData"
            :show-all-levels="false"
            filterable
            clearable
            :append-to-body="false"
            @change="handleChange"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleSearch">查询 </el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置 </el-button>
        </el-form-item>
      </el-form>
      <el-row class="mt2">
        <el-col :span="24">
          <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增 </el-button>
        </el-col>
      </el-row>

      <HkTable
        :table-data="dataList"
        :table-columns="qualtableLabel"
        :show-index="true"
        :operate="true"
        :query-params="queryParams"
        :total="total"
        :loading="loading"
        :table-height="80"
        @changePage="getList"
      >
        <template #outputDataSourceTypeList="scope">
          <span v-for="item in scope.row.outputDataSourceTypeList" :key="item" style="margin-right: 10px">{{
            item
          }}</span>
        </template>
        <template #outputDataSourceNameList="scope">
          <span v-for="item in scope.row.outputDataSourceNameList" :key="item" style="margin-right: 10px">{{
            item
          }}</span>
        </template>
        <!-- <template #configStatus="scope">
          <el-switch
            class="switchStyle"
            v-model="scope.row.configStatus"
            active-text="已发布"
            inactive-text="未发布"
          >
            </el-switch
          >
        </template> -->
        <template #operate="scope">
          <el-button size="mini" type="text" @click="handleUpdate(scope.row)">编辑 </el-button>

          <el-button size="mini" type="text" @click="handleDelete(scope.row)">删除 </el-button>
        </template>
      </HkTable>
    </div>
  </div>
</template>

<script>
import { getTask, delTask } from '@/api/milkyway/taskList'
import { getSourceList } from '@/api/milkyway/repositorypage'
import HkTable from '@/components/Hk-Table/index.vue'
export default {
  name: 'MlkTask',
  components: { HkTable },
  beforeRouteLeave(to, from, next) {
    from.meta.keepAlive = false
    next()
  },
  data() {
    return {
      loading: false,
      // 接入数据源级联选择器来源
      productData: [],
      // 输出数据源来源
      exportsData: [],
      // 级联选择器规则
      propsData: {
        value: 'keyValue',
        label: 'name'
      },
      // 表单数据
      formData: {},
      // 标题
      title: '',
      //  查询
      queryParams: {
        current: 1,
        size: 10
      },
      // 临时数组
      temInputDataSourceType: [],
      temOutputDataSourceType: [],
      // 表格数据来源
      dataList: [],
      // 条数
      total: 0,
      // 表格展示数据
      qualtableLabel: [
        { label: '任务名称', param: 'taskName' },
        { label: '接入数据源', param: 'inputSourceName' },
        { label: '接入数据源类型', param: 'inputSourceType' },
        { label: '作业名称', param: 'jobName' },
        {
          label: '输出数据源',
          param: 'outputDataSourceNameList',
          type: 'slot',
          with: 250
        },
        {
          label: '输出数据源类型',
          param: 'outputDataSourceTypeList',
          type: 'slot',
          with: 250
        }
        // { label: "任务类型", param: "configStatus", type: "slot" },
      ]
    }
  },
  activated() { this.getList() },
  mounted() {
    this.getList()
    this.getProuductType()
  },
  methods: {
    /** 获取产品类型 */
    getProuductType() {
      // 接入数据源
      getSourceList({ type: 0 }).then((res) => {
        res.forEach((item) => {
          this.productData.push({
            name: item.name,
            keyValue: item.keyValue
          })
        })
      })
      // 输出数据源
      getSourceList({ type: 1 }).then((res) => {
        res.forEach((item) => {
          this.exportsData.push({
            name: item.name,
            keyValue: item.keyValue
          })
        })
      })
    },
    /** 级联选事件*/
    handleChange(data) {},
    // 获取数据
    getList(val) {
      this.loading = true
      this.queryParams.inputDataSourceType = this.temInputDataSourceType.toString()
      this.queryParams.outputDataSourceType = this.temOutputDataSourceType.toString()
      this.queryParams = { ...this.queryParams, ...val, scrollToTop: true }

      getTask(this.queryParams).then((res) => {
        this.dataList = res.list
        this.total = res.total
        this.loading = false
        this.queryParams.scrollToTop = false
      })
    },
    // 查询
    handleSearch() {
      this.queryParams.current = 1
      this.getList()
    },
    /* 重置列表*/
    resetQuery() {
      this.queryParams.taskName = ''
      this.temOutputDataSourceType = []
      this.temInputDataSourceType = []
      this.inputDataSourceType = ''
      this.outputDataSourceType = ''
      this.getList()
    },
    // 新增
    handleAdd() {
      this.$router.push(window.prefix + '/mlk/taskAdd')
    },

    // 编辑
    handleUpdate(row) {
      this.$router.push(window.prefix + '/mlk/taskAdd/' + row.id)
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id
      this.$modal
        .confirm('是否确认删除选中的数据项？')
        .then(() => {
          return delTask(id)
        }).then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        }).catch(() => {})
    }
  }
}
</script>

<style lang="scss" scoped>
.box {
  padding: 2px 5px;
  margin-right: 5px;
  background: #e6edf9;
  color: rgba(0, 0, 0, 0.85);
  border-radius: 4px;
}
.app-container {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  ::v-deep .targetTable {
    flex: 1;
    overflow: hidden;
  }
}
::v-deep .el-cascader-menu__wrap{
    height: 100% !important;
    overflow:hidden;
  }
</style>
<style>

 .el-cascader-panel .el-cascader-menu {
    padding: 0 !important;
  }
  </style>
